# _*_coding:utf-8 _*_
import redis
import time

# 连接
# r = redis.Redis(host="127.0.0.1", port=6379, password="Dws666888", db=3, decode_responses=True)
# client.close()
# 连接池
# redis_pool = ConnectionPool(host="10.8.140.151",port=6379,password="123456",db=0,max_connections=16)
# client = Redis(connection_pool=redis_pool)
# print('1001pw=', r.hgetall('1300001'))


# 判断当前时间是否在两个时间之间
def is_between_time(begin_time, end_time):
    now = time.strftime('%H:%M:%S')
    if begin_time <= now <= end_time:
        # print('当前时间在两个时间之间')
        return True
    else:
        return False

# print("当前时间是：", time.strftime('%H:%M:%S'))

'''
黑名单cdr处理函数

hmdgz   接听状态、次数、时效、通话时长小于
{'callstate': '接听', 'nub': '1', 'date': '60', 'calltime': ''}

hmd格式  hmdid {'nub':'1','call':[{'state': '', 'time':''}]}
'''


def hmdsc(cdr, gz, hmdid):
    callstate = gz.get('callstate', '')
    hm = cdr.get('called')  # [3:]
    if (cdr.get('state') in callstate or callstate == 'all') and gz.get('date'):
        gqtime = int(int(gz.get('date')) * 24 * 60 * 60)
        # hmddata[hmdid] = {cdr.get('uPhone'): gqtime}
        # 连接
        r = redis.Redis(host="127.0.0.1", port=6379, password="Dws666888", db=4, decode_responses=True)
        d = r.get(str(hmdid) + ':' + str(hm))
        lnub = 1
        # calldata = []
        if d and eval(d).get('nub') != 'n':
            lnub = int(eval(d).get('nub'))+1
        # 写到redis黑名单
        if not d or (d and eval(d).get('nub') != 'n'):
            d = r.set(str(hmdid) + ':' + str(hm), str({'nub': lnub, 'call': []}))
            e = r.expire(str(hmdid) + ':' + str(hm), gqtime)
        r.close()
    '''
    elif gz.get('nub') and int(gz.get('nub')) > 1:
        if callstate == 'all' and gz.get('date'):
            gqtime = int(time.time()) + (int(gz.get('date') * 24 * 60 * 60))
            # 连接
            r = redis.Redis(host="127.0.0.1", port=6379, password="Dws666888", db=4, decode_responses=True)
            d = r.get(str(hmdid) + ':' + str(hm))
            lnub = 1
            if d:
                lnub = int(eval(d).get('nub'))+1
            d = r.set(str(hmdid) + ':' + str(hm), str({'nub': lnub}))
            e = r.expire(str(hmdid) + ':' + str(hm), gqtime)
            client.close()
        print('多次判断要借助于数据库统计或自维护内存里的通话记录做判断')
    '''


'''判断是否为黑名单函数'''


def hmdpd(hm, hmdid, gz):
    # 连接
    r = redis.Redis(host="127.0.0.1", port=6379, password="Dws666888", db=4, decode_responses=True)
    d = r.get(str(hmdid)+':'+str(hm))
    r.close()
    if (d and (eval(d).get('nub') == 'n' or int(eval(d).get('nub')) >= int(gz.get('nub')))) or (not is_between_time(gz.get('starttime'), gz.get('endtime'))):
        # print('现在返回lj')
        return 'lj'
    else:
        # print('现在返回通过')
        return 0
